package com.googlecode.alogwe.sprockit.unused;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import javax.swing.JTextArea;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JSplitPane;
import javax.swing.border.CompoundBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import com.googlecode.alogwe.sprockit.engine.JSEngine;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Font;
import java.io.IOException;
import java.io.PipedReader;

import javax.swing.ScrollPaneConstants;

public class MainWindow implements Runnable {

	private Thread			thread;
	private JFrame			frmSprockit;
	private JSEngine	engine;
	private JTextArea		consoleInputField;
	private JTextArea		consoleOutputField;
	private PipedReader		consoleReader;

	/**
	 * Create the application.
	 */
	public MainWindow() {
		this(new JSEngine());
	}

	public MainWindow(JSEngine se) {
		this.engine = se;
		initWindow();

		// TODO Use Executors, don't start threads from the constructor;
		thread = new Thread(this);
		thread.start();

	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initWindow() {
		setLookAndFeel();
		frmSprockit = new JFrame();
		frmSprockit.setTitle("Sprockit");
		frmSprockit.setBounds(100, 100, 840, 635);
		frmSprockit.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmSprockit.getContentPane().setLayout(new BorderLayout(0, 0));

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBorder(new CompoundBorder(null, new LineBorder(new Color(51, 98, 140), 1,
				true)));
		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		frmSprockit.getContentPane().add(tabbedPane, BorderLayout.CENTER);

		JSplitPane splitPane = new JSplitPane();
		splitPane.setDividerSize(3);
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		tabbedPane.addTab("Console", null, splitPane, null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		splitPane.setLeftComponent(scrollPane);

		consoleOutputField = new JTextArea();
		consoleOutputField.setFont(new Font("Lucida Sans Typewriter", consoleOutputField.getFont()
				.getStyle(), consoleOutputField.getFont().getSize()));
		consoleOutputField.setWrapStyleWord(true);
		consoleOutputField.setLineWrap(true);
		consoleOutputField.setRows(20);
		consoleOutputField.setTabSize(4);
		scrollPane.setViewportView(consoleOutputField);

		consoleInputField = new JTextArea();
		consoleInputField.setFont(new Font("Lucida Sans Typewriter", consoleInputField.getFont()
				.getStyle(), consoleInputField.getFont().getSize()));
		consoleInputField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {

				int keyCode = e.getKeyCode();

				if (keyCode == KeyEvent.VK_ENTER) {
					String input = consoleInputField.getText();
					consoleInputField.setText("");
				} else if (keyCode == KeyEvent.VK_PAGE_UP) {
					//get previous history;
				} else if (keyCode == KeyEvent.VK_PAGE_DOWN) {
					//get next history;
				}
			}

			@Override
			public void keyReleased(KeyEvent e) {
				int keyCode = e.getKeyCode();
				switch (keyCode) {
					case KeyEvent.VK_ENTER:
						consoleInputField.setText("");
					case KeyEvent.VK_PAGE_UP:
					case KeyEvent.VK_PAGE_DOWN:
						consoleInputField.setCaretPosition(0);
						break;
				}
			}
		});

		consoleInputField.setRows(5);
		consoleInputField.setTabSize(4);
		consoleInputField.setWrapStyleWord(true);
		consoleInputField.setLineWrap(true);
		splitPane.setRightComponent(consoleInputField);
		
		show();
	}

	private void setLookAndFeel() {
		String sNimbusClass = "javax.swing.plaf.nimbus.NimbusLookAndFeel";
		try {
			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
				if (info.getClassName().equalsIgnoreCase(sNimbusClass)) {
					UIManager.setLookAndFeel(info.getClassName());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void hide() {
		frmSprockit.setVisible(false);
	}

	public void show() {
		frmSprockit.setVisible(true);
	}

	@Override
	public void run() {
		try {
			while (true) {
				String s = "";
				while (consoleReader.ready()) {
					s += consoleReader.read();
				}

				writeOutputTextArea(s);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void writeOutputTextArea(String s) {
		consoleOutputField.append("> " + s + "\n");
	}

}
